home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / dos / fgetc.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  3KB  |  134 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: fgetc.c,v 1.3 1996/08/13 13:52:46 digulla Exp $
  4.     $Log: fgetc.c,v $
  5.     Revision 1.3  1996/08/13 13:52:46  digulla
  6.     Replaced <dos/dosextens.h> by "dos_intern.h" or added "dos_intern.h"
  7.     Replaced __AROS_LA by __AROS_LHA
  8.  
  9.     Revision 1.2  1996/08/01 17:40:50  digulla
  10.     Added standard header for all files
  11.  
  12.     Desc:
  13.     Lang: english
  14. */
  15. #include <exec/memory.h>
  16. #include <clib/exec_protos.h>
  17. #include <dos/dosextens.h>
  18. #include "dos_intern.h"
  19.  
  20. /*****************************************************************************
  21.  
  22.     NAME */
  23.     #include <clib/dos_protos.h>
  24.  
  25.     __AROS_LH1(LONG, FGetC,
  26.  
  27. /*  SYNOPSIS */
  28.     __AROS_LHA(BPTR, file, D1),
  29.  
  30. /*  LOCATION */
  31.     struct DosLibrary *, DOSBase, 51, Dos)
  32.  
  33. /*  FUNCTION
  34.     Get a character from a buffered file. Buffered I/O is more efficient
  35.     for small amounts of data but less for big chunks. You have to
  36.     use Flush() between buffered and non-buffered I/O or you'll
  37.     clutter your I/O stream.
  38.  
  39.     INPUTS
  40.     file   - filehandle
  41.  
  42.     RESULT
  43.     The character read or EOF if the file ended or an error happened.
  44.     IoErr() gives additional information in that case.
  45.  
  46.     NOTES
  47.  
  48.     EXAMPLE
  49.  
  50.     BUGS
  51.  
  52.     SEE ALSO
  53.     IoErr(), Flush()
  54.  
  55.     INTERNALS
  56.  
  57.     HISTORY
  58.     29-10-95    digulla automatically created from
  59.                 dos_lib.fd and clib/dos_protos.h
  60.  
  61. *****************************************************************************/
  62. {
  63.     __AROS_FUNC_INIT
  64.     __AROS_BASE_EXT_DECL(struct DosLibrary *,DOSBase)
  65.  
  66.     /* Get pointer to filehandle */
  67.     struct FileHandle *fh=(struct FileHandle *)BADDR(file);
  68.     LONG *result=&((struct Process *)FindTask(NULL))->pr_Result2;
  69.     LONG size;
  70.  
  71.     /* If the file is in write mode... */
  72.     if(fh->fh_Flags&FHF_WRITE)
  73.     {
  74.         /* write the buffer. (In many pieces if the first one isn't enough). */
  75.         UBYTE *pos=fh->fh_Buf;
  76.         while(pos!=fh->fh_Pos)
  77.         {
  78.             size=Write(file,pos,fh->fh_Pos-pos);
  79.             /* An error happened? Return it. */
  80.             if(size<0)
  81.                 return EOF;
  82.             pos+=size;
  83.         }
  84.         /* Reinit filehandle. */
  85.         fh->fh_Flags&=~FHF_WRITE;
  86.         fh->fh_Pos=fh->fh_End=fh->fh_Buf;
  87.     }
  88.     /* No normal characters left. */
  89.     if(fh->fh_Pos>=fh->fh_End)
  90.     {
  91.         /* Check for a pushed back EOF. */
  92.         if(fh->fh_Pos>fh->fh_End)
  93.         {
  94.             /* Reinit filehandle and return EOF. */
  95.             fh->fh_Pos=fh->fh_End;
  96.         *result=0;
  97.             return EOF;
  98.         }
  99.         
  100.         /* Is there a buffer? */
  101.         if(fh->fh_Buf==NULL)
  102.         {
  103.             /* No. Get one. */
  104.             fh->fh_Buf=AllocMem(IOBUFSIZE,MEMF_ANY);
  105.             if(fh->fh_Buf==NULL)
  106.             {
  107.                 /* Couldn't get buffer. Return error. */
  108.                 *result=ERROR_NO_FREE_STORE;
  109.                 return EOF;
  110.             }
  111.             /* Got it. Use it. */
  112.             fh->fh_Flags|=FHF_BUF;
  113.             fh->fh_Size=IOBUFSIZE;
  114.         }
  115.         
  116.         /* Fill the buffer. */
  117.         size=Read(file,fh->fh_Buf,fh->fh_Size);
  118.         
  119.         /* Prepare filehandle for data. */
  120.         if(size<=0)
  121.             size=0;
  122.         fh->fh_Pos=fh->fh_Buf;
  123.         fh->fh_End=fh->fh_Buf+size;
  124.         
  125.         /* No data read? Return EOF. */
  126.         if(!size)
  127.             return EOF;
  128.     }
  129.     
  130.     /* All OK. Get data. */
  131.     return *fh->fh_Pos++;
  132.     __AROS_FUNC_EXIT
  133. } /* FGetC */
  134.